/*
 * HubListReader.java
 *
 * Created on 26 de Abril de 2006, 16:50
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.db;

import hubsniffer.DCHub;
import libjdc.dc.log.Log;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author Daniel Botelho
 */
public class HubListReader {
  
    private DataBaseManager db;
    private ResultSet rs;
    
    /** Creates a new instance of HubListReader */
    public HubListReader(DataBaseManager db) throws SQLException, ClassNotFoundException {
        this.db = db;
    }
    
    private LinkedList<DCHub> getHubList(int hub_type){
        LinkedList<DCHub> hublist = new LinkedList<DCHub>();
        
        rs = null;
        try {
            rs = db.getSql_stm().executeQuery("SELECT * from hub where hub_is_active = true and hub_type_id="+hub_type);
        } catch (SQLException se) {
            Log.out.println("We got an exception while executing our query:" +
                    "that probably means our SQL is invalid");
            se.printStackTrace();
            System.exit(1);
        }
        
        try {
            while (rs.next()) {          
                hublist.add(new DCHub(new Integer(rs.getString(DCHub.HUB_ID)).intValue(),rs.getString(DCHub.HUB_ADDRESS),new Integer(rs.getString(DCHub.HUB_PORT)).intValue(),new Integer(rs.getString(DCHub.HUB_TYPE_ID)).intValue()));
            }
        } catch (SQLException se) {
            Log.out.println("We got an exception while getting a result:this " +
                    "shouldn't happen: we've done something really bad.");
            se.printStackTrace();
            System.exit(1);
        }
        
        return hublist;
    }
    
    public LinkedList<DCHub> getHubListInternacional(){
        return getHubList(2);
    }
    
    public LinkedList<DCHub> getHubListNacional(){
        return getHubList(1);
    }
    
    public LinkedList<DCHub> getAllActiveHubs(){
        LinkedList<DCHub> hublist = new LinkedList<DCHub>();
        
        rs = null;
        try {
            rs = db.getSql_stm().executeQuery("SELECT * from hub where hub_is_active = true");
        } catch (SQLException se) {
            Log.out.println("We got an exception while executing our query:" +
                    "that probably means our SQL is invalid");
            se.printStackTrace();
            System.exit(1);
        }
        
        try {
            while (rs.next()) {          
                hublist.add(new DCHub(new Integer(rs.getString(DCHub.HUB_ID)).intValue(),rs.getString(DCHub.HUB_ADDRESS),new Integer(rs.getString(DCHub.HUB_PORT)).intValue(),new Integer(rs.getString(DCHub.HUB_TYPE_ID)).intValue()));
            }
        } catch (SQLException se) {
            Log.out.println("We got an exception while getting a result:this " +
                    "shouldn't happen: we've done something really bad.");
            se.printStackTrace();
            System.exit(1);
        }
        
        return hublist;
    }
}